<?php
namespace app\api\controller\customer;
use think\File;
use think\facade\Env;
use app\common\model\user\User as UserModel;
use app\common\model\company\Company;
use app\common\model\company\Contract;
use app\common\model\orderdata\Goods;
use app\common\model\quotation\QuotationList;
use app\common\model\buss\Buss;
use app\common\model\power\Rolepowerlnk;
use app\api\controller\Base;
use app\common\model\power\Rolepower;
use app\common\model\orderhawa\HawaList;
use app\common\model\customer\Customer;
use app\common\model\customer\Customervisit;
class Manacustomer extends Base
{
	/**
	 * 产生jwt加密串，参考session
	 */
    public function initialize(){
		parent::initialize();
		self::is_login();
		// Rolepower::powerguard(201102);
	}

	/**
	 * 基础信息
	 * @return [type] [description]
	 */
	public function getinfo(){
		$id = input('id');
		if(empty($id)){
			return myerr('缺少参数，请重新尝试');
		}
		$data = Customer::user_getinfo($id,'mana',[]);
		if(empty($data)){
			return myerr('没有找到此客户');
		}
		return myok($data);
	}

	/**
	 * 查看管理通话列表
	 * @return [type] [description]
	 */
	public function tellist(){
		$data = input('get.');
 		$data['Type'] = 'customer';
 		//获取客户咨询记录中对应的id
 		$cuidlist = Customervisit::distinct(true);
 		if(!empty($data['bussid'])){
 			$cuidlist = $cuidlist->where('BussinessId',(int)$data['bussid']);
 		}
 		$culist = $cuidlist->where('AddTime','>=',$data['startValue'])->where('AddTime','<=',$data['endValue'].' 25:00')->order('AddTime desc')->column('Content','CustomerId');
 		$cuidlist=[];
 		foreach ($culist as $key => $value) {
 			$cuidlist[] = $key;
 		}

		$data = init_where($data,['time'=>false,'bussid'=>false]);
 		$data['where'][]=['CustomerId','in',$cuidlist];
		$list = Customer::usergetlist($data,'mana');
		foreach ($list['list'] as $key => $value) {
			//客户回访记录
			$string = $culist[$value['CustomerId']];
			$string = str_replace("\n", "", $string);
			$string = str_replace(" ", "", $string);
			$list['list'][$key]['customertext'] = $string;
			if(!empty($value['guard_state'])){
				$list['list'][$key]['GState'] = '保护中';
			}else{
				$list['list'][$key]['GState'] = '';
			}

			if(!empty($value['bussinfo']['Name'])){
				$list['list'][$key]['Bname'] = $value['bussinfo']['Name'];
			}else{
				$list['list'][$key]['Bname'] ='暂无';
			}
		}
		$list['tr']['title']  = ['客户名称','业务员','状态','查看信息','最后沟通时间','登记时间','企业分类','产废规模','客户保护'];
		$list['tr']['key'] = ['Name','Bname','State','CustomerId','LastTime','AddTime', 'Type', 'Scale','GState','customertext'];
		$list['tr']['show'] = [true,true,true,true,false,false,false,false,false];
		return myok($list);
	}

	/**
	 * 查看管理列表
	 * @return [type] [description]
	 */
	public function list(){
		$data = input('get.');
		$data['Type'] = 'customer';
		$data = init_where($data);
		$list = Customer::usergetlist($data,'mana');
		foreach ($list['list'] as $key => $value) {
			if(!empty($value['guard_state'])){
				$list['list'][$key]['GState'] = '保护中';
			}else{
				$list['list'][$key]['GState'] = '';
			}

			if(!empty($value['bussinfo']['Name'])){
				$list['list'][$key]['Bname'] = $value['bussinfo']['Name'];
			}else{
				$list['list'][$key]['Bname'] ='暂无';
			}
		}
		$list['tr']['title']  = ['客户名称','业务员','状态','查看信息','沟通时间','登记时间','企业分类','产废规模','客户保护'];
		$list['tr']['key'] = ['Name','Bname','State','CustomerId','LastTime','AddTime', 'Type', 'Scale','GState'];
		$list['tr']['show'] = [true,true,true,true,false,false,false,false,false];
		return myok($list);
	}

	public function maplist(){
		$key = trim(input('searchkey'));
		if($key=="%"){return moerr('非法参数');}
		$idata = input('get.');
		//定位
		if(!empty($idata['lat'])){
			$sdata['field'] = [str_distance($idata['lat'],$idata['lon'])];
			$sdata['order'] = 'distance';
		}
		
		//没有定位直接显示关键词
		if(!empty($key)){
			$sdata = ['where'=>[['Name','like','%'.$key.'%']]];
		}else{
			$sdata['limit'] = '999';
		}
		$sdata['Type'] = 'company';
		$data = Company::usergetlist($sdata,'mana',false);
		return myok($data);	
	}

	/**
	 * 判断账号是否登录
	 * @return boolean [description]
	 */
	private function is_login(){
		$data = UserModel::is_sessionlogin();
		if (!$data['code']) {
			$data['options'] = 'relogin';
			json($data)->send();
			exit;
		}
	}
}
